package hsp.admin.app.base.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hsp.admin.api.ApiResult;
import hsp.admin.app.base.dto.PasswordDto;
import hsp.admin.app.base.service.IUserService;
import hsp.admin.app.base.vo.UserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@Tag(name = "UserController", description = "用户管理")
@RestController
@RequestMapping("base/user")
public class UserController {

    private final IUserService service;

    @Autowired
    public UserController(
        IUserService service
    ){
        this.service = service;
    }

    @Operation(summary = "获取用户")
    @GetMapping("{id}")
    public ApiResult<UserVO> getUser(@PathVariable("id") long id){
        UserVO vo = service.getVOById(id);
        return ApiResult.success(vo);
    }

    @Operation(summary = "获取用户分页")
    @GetMapping("page/{page}/{size}")
    public ApiResult<Page<UserVO>> getUserPage(@PathVariable int page, @PathVariable int size, UserVO vo){
        Page<UserVO> records = service.pageVO(new Page<>(page, size), vo);
        return ApiResult.success(records);
    }

    @Operation(summary = "编辑用户")
    @PostMapping()
    public ApiResult<?> postOrPutUser(@RequestBody UserVO vo){
        service.saveOrUpdate(vo);
        return ApiResult.success();
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("{id}")
    public ApiResult<?> deleteUser(@PathVariable("id") long id){
        service.removeById(id);
//        service.removeUserInfoById(id);
        return ApiResult.success();
    }

    @Operation(summary = "修改密码")
    @PutMapping("password")
    public ApiResult<?> putPassword(@Validated @RequestBody PasswordDto passwordDto){
        service.updatePassword(passwordDto);
        return ApiResult.success();
    }

    @Operation(summary = "重置密码")
    @PutMapping("password/reset/{id}")
    public ApiResult<?> resetPassword(@PathVariable long id){
        service.resetPassword(id);
        return ApiResult.success();
    }

}
